import java.util.InputMismatchException;
import java.util.Scanner;

class Login{
    public String userName = "Aileen";
    public String passWord = "123456";


    public  void logIn(String userName , String passWord){
        if(!this.userName.equals(userName)){
//            System.out.println("用户名错误！");
            throw new UserNameException("用户名错误！");
        }

        if(!this.passWord.equals(passWord)){
//            System.out.println("密码错误!");
            throw  new PassWordException("密码错误!");
        }
    }
}

public class Test {
    public static void main(String[] args) {
        try{
            Login login = new Login();
            login.logIn("Aileen","abcd");
        }catch (UserNameException e){
            e.printStackTrace();
            System.out.println("捕捉到了用户名错误异常。。。");
        }catch (PassWordException e){
            e.printStackTrace();
            System.out.println("捕捉到了密码错误异常。。。");
        }

        System.out.println("程序继续执行");

    }

    //-----------------------------------------------------------------------
    public static int func2() {
        Scanner scanner = new Scanner(System.in);
        try {
            int a = 10;
            return a;
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("捕获InputMismatchException异常...");
        } finally {
            System.out.println("finally被执行了");
            scanner.close();
            return 99;
        }
    }

    public static void main6(String[] args) {
        System.out.println(func2());
    }

    //-----------------------------------------------------------------------
    public static void main5(String[] args) {
        Scanner scanner = new Scanner(System.in);
        try{
            int[] array = {1,2,3,4};
            System.out.println(array[10]);

            int a = scanner.nextInt();
            System.out.println(a);
        }catch (NullPointerException e){
            e.printStackTrace();
            System.out.println("捕获NullPointerException异常...");
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("捕获ArrayIndexOutOfBoundsException异常...");
        }finally {
            System.out.println("finally被执行了");
            scanner.close();
        }
        System.out.println("after");
    }

    //--------------------------------------------------------------------
    public static void main4(String[] args) {
        int[] arr = {1,2,3};
        try{
            System.out.println("before");
            arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        }catch(NullPointerException e){//空指针异常，属于异常的子类
            e.printStackTrace();
            System.out.println("我是空指针异常~");
        }
        catch(Exception e){//可捕获到所有异常，所有异常的父类
            e.printStackTrace();
        }
        System.out.println("after try catch");
    }

    //-----------------------------------------------------------------------------------------------
    public static void main3(String[] args) {
        try {
            int[] array = {1, 2, 3, 4, 5};
            System.out.println(array[9]);

        }catch (NullPointerException | ArrayIndexOutOfBoundsException e){  //可以通过|分割捕获多个异常，但是缺点是无法知道具体是哪一个
            e.printStackTrace();
            System.out.println("捕获到空指针 | 数组越界异常......");
        }
        System.out.println("Aileen");
    }
    //----------------------------------------------------------------------------------------------
    public static void main2(String[] args) {

        try{
            int[] array = {1,2,3,4,5};
            System.out.println(array[9]);

            System.out.println("Aileen");

        }catch (NullPointerException e){
            e.printStackTrace();
            System.out.println("捕获到了空指针异常。。。");
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("捕获到了数组越界异常。。。");
        }

        System.out.println("after");
    }

    //------------------------------------------------------
    public static void func() throws NullPointerException{
        int[] array = null;
        System.out.println(array.length);
    }

    public static void main1(String[] args){
        try {
            func();//将可能出现异常的代码放这
        } catch (NullPointerException e) {//捕获对应的异常
            e.printStackTrace();
            System.out.println("处理异常");
        }
        System.out.println("after");
    }
}
